package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.UserInfoUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.CartForm;
import com.cskaoyan.shopping.service.ICartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 购物车controller
 * 创建日期: 2022/04/24 11:36
 *
 * @author HanWeiqi
 */

@RestController
@RequestMapping("shopping")
@Slf4j
public class CartController {

    @Autowired
    ICartService iCartService;

    @PostMapping("/carts")
    public ResponseData addCarts(@RequestBody CartForm cartForm, HttpServletRequest servletRequest) {
        Long uid = UserInfoUtil.checkLoginAndGetUserId(servletRequest);

        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(uid);
        addCartRequest.setItemId(cartForm.getProductId());
        addCartRequest.setNum(cartForm.getProductNum());

        AddCartResponse addCartResponse = iCartService.addToCart(addCartRequest);

        if (ShoppingRetCode.SUCCESS.getMessage().equals(addCartResponse.getMsg())) {
            return new ResponseUtil().setData(addCartResponse.getMsg());
        }
        return new ResponseUtil().setErrorMsg(addCartResponse.getMsg());
    }


    @GetMapping("/carts")
    public ResponseData getCarts(HttpServletRequest servletRequest){
        Long uid = UserInfoUtil.checkLoginAndGetUserId(servletRequest);
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);


        CartListByIdResponse cartListByIdResponse = iCartService.getCartListById(cartListByIdRequest);

        if (ShoppingRetCode.SUCCESS.getMessage().equals(cartListByIdResponse.getMsg())) {
            return new ResponseUtil().setData(cartListByIdResponse.getCartProductDtos());
        }
        return new ResponseUtil().setErrorMsg(cartListByIdResponse.getMsg());
    }

    @PutMapping("/carts")
    public ResponseData updateCarts(@RequestBody CartForm cartForm, HttpServletRequest servletRequest) {
        Long uid = UserInfoUtil.checkLoginAndGetUserId(servletRequest);

        UpdateCartNumRequest updateCartNumRequest = new UpdateCartNumRequest();
        updateCartNumRequest.setUserId(uid);
        updateCartNumRequest.setItemId(cartForm.getProductId());
        updateCartNumRequest.setNum(cartForm.getProductNum());
        updateCartNumRequest.setChecked(cartForm.getChecked());

        UpdateCartNumResponse updateCartNumResponse = iCartService.updateCartNum(updateCartNumRequest);

        if (ShoppingRetCode.SUCCESS.getMessage().equals(updateCartNumResponse.getMsg())) {
            return new ResponseUtil().setData(updateCartNumResponse.getMsg());
        }
        return new ResponseUtil().setErrorMsg(updateCartNumResponse.getMsg());
    }

    @PutMapping("/items")
    public ResponseData cartsChecked(@RequestBody CartForm cartForm,HttpServletRequest servletRequest){
        Long uid = UserInfoUtil.checkLoginAndGetUserId(servletRequest);
        CheckAllItemRequest checkAllItemRequest = new CheckAllItemRequest();
        checkAllItemRequest.setUserId(uid);
        checkAllItemRequest.setChecked(cartForm.getChecked());

        CheckAllItemResponse checkAllItemResponse = iCartService.checkAllCartItem(checkAllItemRequest);

        if (ShoppingRetCode.SUCCESS.getMessage().equals(checkAllItemResponse.getMsg())) {
            return new ResponseUtil().setData(checkAllItemResponse.getMsg());
        }
        return new ResponseUtil().setErrorMsg(checkAllItemResponse.getMsg());

    }

    @DeleteMapping("/items/{uid}")
    public ResponseData deleteCheckedItems(@PathVariable("uid") Long uid,HttpServletRequest servletRequest){
        Long userId = UserInfoUtil.checkLoginAndGetUserId(servletRequest);
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(userId);

        DeleteCheckedItemResposne deleteCheckedItemResposne = iCartService.deleteCheckedItem(deleteCheckedItemRequest);
        if (ShoppingRetCode.SUCCESS.getMessage().equals(deleteCheckedItemResposne.getMsg())) {
            return new ResponseUtil().setData(deleteCheckedItemResposne.getMsg());
        }
        return new ResponseUtil().setErrorMsg(deleteCheckedItemResposne.getMsg());
    }


    @DeleteMapping("/carts/{uid}/{pid}")
    public ResponseData deleteItems(@PathVariable("uid") Long uid,@PathVariable("pid") Long pid,HttpServletRequest servletRequest){
        Long userId = UserInfoUtil.checkLoginAndGetUserId(servletRequest);
        DeleteCartItemRequest deleteCheckedItemRequest = new DeleteCartItemRequest();
        deleteCheckedItemRequest.setUserId(userId);
        deleteCheckedItemRequest.setItemId(pid);

        DeleteCartItemResponse deleteCartItemResponse = iCartService.deleteCartItem(deleteCheckedItemRequest);

        if (ShoppingRetCode.SUCCESS.getMessage().equals(deleteCartItemResponse.getMsg())) {
            return new ResponseUtil().setData(deleteCartItemResponse.getMsg());
        }
        return new ResponseUtil().setErrorMsg(deleteCartItemResponse.getMsg());
    }


}
